package com.johnson.news.model.util;


import android.app.ProgressDialog;
import android.content.Context;

import com.youmbe.library.util.MyToast;
import com.youmbe.library.view.MyProgressDialog;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class Transformer {

    /**
     *
     ** @param <T>         泛型
     * @return 返回Observable
     */
    public static <T> ObservableTransformer<T, T> switchSchedulers() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream
                        .subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {

                            }
                        })
                        .doOnError(new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                throwable.printStackTrace();
                            }
                        })
                        .subscribeOn(AndroidSchedulers.mainThread())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    /**
     * 带参数  显示loading对话框
     ** @param <T>         泛型
     * @return 返回Observable
     */
    public static <T> ObservableTransformer<T, T> switchSchedulersAndLoading(final Context context) {
        return new ObservableTransformer<T, T>() {
            ProgressDialog progressDialog;
            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream
                        .subscribeOn(Schedulers.io())
                        .doOnSubscribe(new Consumer<Disposable>() {//doOnSubscribe后边的subscribeOn决定其代码执行的线程
                            @Override
                            public void accept(Disposable disposable) throws Exception {
//                                if(!disposable.isDisposed()) {
//                                    progressDialog = MyProgressDialog.show(context, "", "数据加载中...");
//                                }
                            }
                        })
                        .subscribeOn(AndroidSchedulers.mainThread())
                        .doFinally(new Action() {
                            @Override
                            public void run() throws Exception {
//                                if(progressDialog != null && progressDialog.isShowing())
//                                    progressDialog.dismiss();
                            }
                        })
                        .doOnError(new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                MyToast.showToast("获取数据失败，请稍后重试。", context);
                                throwable.printStackTrace();
                            }
                        })
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

}